from urllib import request
from urllib.request import urlretrieve
import os
import time


class HtmlDownLoader(object):
    def downLoad(self, url):
        if url is None:
            return
        header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
        }
        req = request.Request(url, headers=header)
        resp = request.urlopen(req)
        if resp.getcode() != 200:
            return
        content = resp.read().decode("utf-8")
        f= open("test.html","w",encoding='utf-8')
        f.write(str(content))
        f.close()
        return content

    def create_file(self,file_path):
        try:
            if not os.path.exists(file_path):
                os.makedirs(file_path)
        except:
            print("创建文件失败！")

    def img_download(self,img_nodes):
        file_path = "img"
        for img_node in img_nodes:
            try:
                img_url = img_node.attrs['src']
                # print(img_url)
                img_name = img_node.attrs['alt']
                print("正在下载图片%s" % img_name)
                self.create_file(file_path)
                # time.sleep(0.1)
                urlretrieve(img_url,'%s/%s.jpg' % (file_path,img_name))
            except:
                print("下载图片%s失败" %img_node)
